package com.windows
{
	import com.bases.TPanel;
	import com.bases.TUIComponent;
	import com.Event.SCWindowEvent;
	import com.xjw.CenterLocation;
	import com.xjw.adapte.AdapteManager;
	import com.xjw.adapte.IAdapte;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 窗口管理 
	 * @author petehan
	 * 
	 */	
	public class TWindowsManager implements IAdapte
	{
		private static var _instance:TWindowsManager;
		
		private var base:Sprite;
		private var mask:TUIComponent;
		
		private var stageW:Number;
		private var stageH:int;
		
		private var windows:Array=[];
		
		public function TWindowsManager(se:SingleStance)
		{
			if(se==null){
				throw new Error('请使用单例');
			}
		}
		
		public function adapte(w:Number,h:Number):void{
			mask.graphics.clear();
			mask.graphics.beginFill(0x000000,0.6);
			mask.graphics.drawRect(0,0,w,h);
			mask.graphics.endFill();
		}
		
		public static function getInstance():TWindowsManager{
			if(_instance==null){
				_instance=new TWindowsManager(new SingleStance());
				//_instance.setBaseContainer(TConfig.getInstance().stage);
			}
			return _instance;
		}
		
		public function setBaseContainer(container:Sprite):void{
			base=container;
			buildMask();
			base.addEventListener(SCWindowEvent.RESIZE,onWindowResize);
		}
		
		private function buildMask():void{
			if(mask){
				mask.removeEventListener(Event.ADDED_TO_STAGE,onAddMask);
				mask.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveMask);
				if(mask.parent){
					mask.parent.removeChild(mask);
				}
				mask.graphics.clear();
				mask=null;
			}
			mask=new TUIComponent();
			mask.mouseEnabled=true;
			this.stageH=this.base.stage.stageHeight;
			this.stageW=this.base.stage.stageWidth;
			
			mask.graphics.beginFill(0x000000,0.1);
			mask.graphics.drawRect(0,0,base.stage.stageWidth,base.stage.stageHeight);
			mask.graphics.endFill();
			
			mask.addEventListener(Event.ADDED_TO_STAGE,onAddMask);
			mask.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveMask);
		}
		
		private function onAddMask(event:Event):void{
			AdapteManager.getInstance().addAdpateControl(this);
		}
		
		private function onRemoveMask(event:Event):void{
			AdapteManager.getInstance().removeAdapteControl(this);
		}
		
		public function addWindow(window:Sprite,parent:Sprite=null,needCenter:Boolean=true,maskalpha:Number=0.1):void{
			windows.push(window);
			if(base.contains(mask)){
				base.setChildIndex(mask,base.numChildren-1);
			}else{
				base.addChild(mask);
			}
			mask.alpha=maskalpha;
			base.addChild(window);
			if(needCenter){
				var center:CenterLocation=new CenterLocation(window);
				window.x=center.centerX;
				window.y=center.centerY;
			}
		}
		
		public function removeWindow(window:DisplayObject):void{
			var t:int=windows.indexOf(window);
			if(t>=0){
				windows.splice(t,1);
				base.removeChild(window);
			}
			if(windows.length>0){
				//窗口至最顶层
				base.setChildIndex(windows[windows.length-1],base.numChildren-1);
			}else{
				base.removeChild(mask);
			}
		}
		
		private function onWindowResize(evt : SCWindowEvent) : void {
			if(!(evt.target is TPanel))return;
			var window : TPanel = evt.target as TPanel;
			window.setPos(base.stage.stageWidth / 2 - window.width / 2, base.stage.stageHeight / 2 - window.height / 2 + 6);
		}
		
	}
}

class SingleStance{}